package com.tebiecloud.retail.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserExpand;
import com.tebiecloud.order.client.model.vo.OrderRetailList;
import com.tebiecloud.retail.client.model.entity.RetailOrder;
import com.tebiecloud.retail.client.model.vo.RetailList;
import com.tebiecloud.retail.client.model.vo.RetailVO;
import com.tebiecloud.retail.server.mapper.RetailOrderMapper;
import com.tebiecloud.retail.server.service.RetailOrderService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import com.tebiecloud.retail.server.service.feign.MpweixinUserServiceClient;
import com.tebiecloud.retail.server.service.feign.OrderServiceClient;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 分销的订单 服务实现类
 * @author tebie
 * @date 2019-09-17
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RetailOrderServiceImpl extends BaseServiceImpl<RetailOrderMapper, RetailOrder> implements RetailOrderService {

    @Autowired
    RetailOrderMapper retailOrderMapper;

    @Autowired
    OrderServiceClient orderServiceClient;

    @Autowired
    MpweixinUserServiceClient mpweixinUserServiceClient;


    @Override
    public RetailVO selectRetailList(long userId,String openid) {
        RetailVO retailVO = new RetailVO();
        List<RetailList> retailListList = new ArrayList<>();
        //获取待核销和已核销的订单
        QueryWrapper<RetailOrder> retailOrderQueryWrapper = new QueryWrapper<>();
        retailOrderQueryWrapper.and(wrapper -> wrapper.eq("pone_user_id", userId)
                .or().eq("ptwo_user_id", userId)
                .or().eq("pthree_user_id", userId));
        List<Integer> statuslist = new ArrayList<>();
        statuslist.add(2);
        statuslist.add(4);
        retailOrderQueryWrapper.in("status",statuslist);
        retailOrderQueryWrapper.orderByDesc("ct");
        List<RetailOrder> retailOrderList = retailOrderMapper.selectList(retailOrderQueryWrapper);
        int clinchAmount = 0; //赚的钱
        int clinchOrderNumber = 0; //核销单数

        for(RetailOrder retailOrder:retailOrderList){
            if(retailOrder.getStatus().equals(4)){
                clinchOrderNumber = clinchOrderNumber + 1;
                clinchAmount = clinchAmount + retailOrder.getProfit();
            }
            RetailList retailList = new RetailList();
            retailList.setOrderId(retailOrder.getOrderId());
            retailList.setProfit(retailOrder.getProfit());
            retailList.setStatus(retailOrder.getStatus());
            //查询订单信息
            ResultBody<OrderRetailList> orderRetailListResultBody = orderServiceClient.selectOrderVo(retailOrder.getOrderId());
            if(orderRetailListResultBody != null && orderRetailListResultBody.getCode() == ResultEnum.OK.getCode()){
                OrderRetailList orderRetailList = orderRetailListResultBody.getData();
                if(orderRetailList!=null){
                    retailList.setCt(orderRetailList.getCt());
                    retailList.setStoreName(orderRetailList.getStoreName());
                    retailList.setPrice(orderRetailList.getPrice());
                }
            }
            //获取分享给谁的个人信息
            ResultBody<MpweixinUserExpand> mpweixinUserExpandResultBody = mpweixinUserServiceClient.selectUserInfo(retailOrder.getUserId(),null);
            if(mpweixinUserExpandResultBody!=null && mpweixinUserExpandResultBody.getCode() == ResultEnum.OK.getCode()){
                MpweixinUserExpand mpweixinUserExpand = mpweixinUserExpandResultBody.getData();
                if(mpweixinUserExpand!=null){
                    retailList.setHeadimgurl(mpweixinUserExpand.getHeadimgurl());
                    retailList.setNickname(mpweixinUserExpand.getNickname());
                }
            }
            retailListList.add(retailList);
        }
        retailVO.setClinchAmount(clinchAmount);
        retailVO.setClinchOrderNumber(clinchOrderNumber);
        retailVO.setRetailList(retailListList);
        return retailVO;
    }
}
